package com.faf.ui;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.faf.bean.Detail;
import com.faf.bean.Food;
import com.faf.bean.Orders;
import com.faf.dao.OrdersDao;
import com.faf.dao.impl.DetailDaoImpl;
import com.faf.dao.impl.FoodDaoImpl;
import com.faf.dao.impl.OrdersDaoImpl;
import com.faf.dao.impl.UserDaoImpl;

/**
 * Servlet implementation class MyOrders
 */
@WebServlet("/MyOrders")
public class MyOrders extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyOrders() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8"); 
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		OrdersDaoImpl ordersDao = new OrdersDaoImpl();
		FoodDaoImpl foodDao = new FoodDaoImpl();
		DetailDaoImpl detailDao = new DetailDaoImpl();
		UserDaoImpl userDao = new UserDaoImpl();
		
		int iPage = null == request.getParameter("page") ? 1 : Integer.parseInt(request.getParameter("page"));
		int iCountPerPage = 3;
		int iCountFood = 0;
		int iCountPage = 0;
		
		int userId = (int)request.getSession().getAttribute("id");
		
		List<Orders> user_orders_orderlist = null;
		
		try {
			user_orders_orderlist = ordersDao.select(OrdersDao.IdType.UserId, String.valueOf(userId),iPage,iCountPerPage);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		
		HashMap<Integer, Food> user_orders_id2food = new HashMap<Integer, Food>();
		HashMap<String, List<Detail>> user_orders_detaillist = new HashMap<String, List<Detail>>();  
		
		for(Orders order : user_orders_orderlist) {
			List<Detail> detaillist = detailDao.getDetailListByorderNumber(order.getOrdersNumber());
			for(Detail detail : detaillist) {
				try {
					user_orders_id2food.put(detail.getFoodId(), foodDao.getFoodByFoodID(detail.getFoodId()));
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			user_orders_detaillist.put(order.getOrdersNumber(), detaillist);
		}
		
		try {
			iCountFood = ordersDao.count(OrdersDao.IdType.UserId, String.valueOf(userId));
			iCountPage = iCountFood / iCountPerPage + (iCountFood % iCountPerPage == 0 ? 0 : 1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		request.setAttribute("pre_myorder_dat", true);
		request.setAttribute("user_orders_id2food", user_orders_id2food);
		request.setAttribute("user_orders_detaillist", user_orders_detaillist);
		request.setAttribute("user_orders_orderlist", user_orders_orderlist);
		request.setAttribute("page", iPage);
		request.setAttribute("page-count", iCountPage);
		
		request.getRequestDispatcher("/UserManagement/user_center_order.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
